//
//  RepositoryNodeViewController.m
//  Alfresco
//
//  Created by Michael Muller on 9/1/09.
//  Copyright 2010 Zia Consulting.  Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
//

#import "RepositoryNodeViewController.h"
#import "DocumentViewController.h"
#import "RepositoryItemTableViewCell.h"
#import "Utility.h"
#import "RepositoryItem.h"
#import "FolderItemsDownload.h"

@implementation RepositoryNodeViewController

@synthesize guid;
@synthesize progressBar;
@synthesize itemDownloader;
@synthesize children;

- (void)dealloc {
	[guid release];
	[progressBar release];
	[itemDownloader release];
	[children release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [children count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	RepositoryItemTableViewCell *cell = (RepositoryItemTableViewCell *) [tableView dequeueReusableCellWithIdentifier:RepositoryItemCellIdentifier];
    if (cell == nil) {
		NSArray *nibItems = [[NSBundle mainBundle] loadNibNamed:@"RepositoryItemTableViewCell" owner:self options:nil];
		cell = [nibItems objectAtIndex:0];
		NSAssert(nibItems, @"Failed to load object from NIB");
    }
    
	RepositoryItem *child = [children objectAtIndex:[indexPath row]];
	
	cell.filename.text = child.title;
	cell.details.text = [[NSString alloc] initWithFormat:@"%@ %@", child.lastModifiedBy, child.lastModifiedDate];
	if ([child isFolder]) {
		UIImage * img = [UIImage imageNamed:@"folder.png"];
		cell.imageView.image  = img;
	}
	else {
		cell.imageView.image = imageForFilename(child.title);
	}
	
	cell.accessoryType = [child isFolder] ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;

	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

	RepositoryItem *child = [children objectAtIndex:[indexPath row]];
	
	if ([child isFolder]) {
		[self.itemDownloader.urlConnection cancel];
		
		FolderItemsDownload *down = [[FolderItemsDownload alloc] initWithGUID:child.guid delegate:self];
		self.itemDownloader = down;
		down.parentTitle = child.title;
		[down start];
		[down release];
	}
	else {
		self.progressBar = [DownloadProgressBar createAndStartWithURL:[NSURL URLWithString:child.contentLocation] delegate:self message:@"Downloading Document" filename:child.title];
	}
}

- (void) asyncDownloadDidComplete:(AsynchonousDownload *)async {
	
	FolderItemsDownload *fid = (FolderItemsDownload *) async;

	// create a new view controller for the list of repository items (documents and folders)
	RepositoryNodeViewController *vc = [[RepositoryNodeViewController alloc] initWithNibName:nil bundle:nil];
	
	vc.title = fid.parentTitle;
	vc.children = fid.children;
	
	// push that view onto the nav controller's stack
	[self.navigationController pushViewController:vc animated:YES];
	[vc release];
}

- (void) asyncDownload:(AsynchonousDownload *)async didFailWithError:(NSError *)error {
}

- (void) download:(DownloadProgressBar *)down completeWithData:(NSData *)data {

	DocumentViewController *doc = [[DocumentViewController alloc] initWithNibName:@"DocumentViewController" bundle:[NSBundle mainBundle]];
	
	doc.fileData = data;
	doc.fileName = down.filename;
	
	doc.hidesBottomBarWhenPushed = YES;
	
	[self.navigationController pushViewController:doc animated:YES];
	
	[doc release];
}

@end
